package cn.cyh.pet.controller;

import cn.cyh.basic.exception.BusinessException;
import cn.cyh.basic.uitls.AJaxResult;
import cn.cyh.basic.uitls.LoginContext;
import cn.cyh.basic.uitls.PageList;
import cn.cyh.pet.domain.Pet;
import cn.cyh.pet.domain.SearchMasterMsg;
import cn.cyh.pet.query.SearchMasterMsgQuery;
import cn.cyh.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.loadAll();
    }
    @PostMapping("/publish")
    public AJaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request,Long loginInfoid){
        try {

            searchMasterMsgService.publish(searchMasterMsg, LoginContext.currentLongInfo(request).getId());
            return AJaxResult.myself();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("系统繁忙");
        }

    }
    // 查询待处理消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            return searchMasterMsgService.pending(query, LoginContext.currentLongInfo(request).getId());
        }  catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }
    // 查询已处理消息
    // 查询待处理消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            return searchMasterMsgService.processed(query, LoginContext.currentLongInfo(request).getId());
        }  catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    // 用户自己的消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {

            return searchMasterMsgService.queryUserMsg(query, LoginContext.currentLongInfo(request).getId());
        }  catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 处理寻主
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AJaxResult handle(@RequestBody Pet pet ,HttpServletRequest request){
        try {
            searchMasterMsgService.handle(pet, LoginContext.currentLongInfo(request).getId());
            return AJaxResult.myself();
        }  catch (BusinessException e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg(e.getMessage());
        }
          catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("系统繁忙");
        }
    }

}
